package com.sunday.aomicaichang.ui.product;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.adpter.ProductMenuAdapter;
import com.sunday.member.base.BaseFragment;
import com.sunday.member.entity.product.ProductDetail;
import com.sunday.member.http.MemberClient;
import com.sunday.member.utils.SignUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/7.
 */

public class ProductMenuFragment extends BaseFragment {
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    private String tit;

    private ProductMenuAdapter adapter;
    private List<ProductDetail.CookBooksBean> dataSet = new ArrayList<>();

    private LinearLayoutManager layoutManager;
    private long id;

    public static ProductMenuFragment newInstance(String tit,long id) {
        ProductMenuFragment fragment = new ProductMenuFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tit", tit);
        bundle.putLong("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_product_menu, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            tit = getArguments().getString("tit");
            id=getArguments().getLong("id");
        }
        initRecylerView();
        getProductDetail();
    }

    private void initRecylerView() {
        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        layoutManager = new LinearLayoutManager(getActivity());
        adapter = new ProductMenuAdapter(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter.setmData(dataSet);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .drawable(R.drawable.shape_divider)
                .build());

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getProductDetail();
            }
        });
    }
    private void getProductDetail() {
        int villageId = SharePerferenceUtils.getIns(mContext).getInt(com.sunday.aomicaichang.constValues.ConstValue.AREA, 0);
        String nonce = MD5Utils.random();//随机数
        String[] str1 = new String[]{"productId", "villageId", "nonce"};
        String[] str2 = new String[]{String.valueOf(id), String.valueOf(villageId), nonce};
        showLoadingDialog(0);
        Call<ResultDO<ProductDetail>> call = MemberClient.getApiAdapter().getProductDetail(id, villageId, nonce, SignUtil.getSign(str1, str2));
        call.enqueue(new Callback<ResultDO<ProductDetail>>() {
            @Override
            public void onResponse(Call<ResultDO<ProductDetail>> call, Response<ResultDO<ProductDetail>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() == null) {
                        return;
                    }

                    ProductDetail productDetail = response.body().getResult();
                    if(productDetail==null)
                        return;
                    if(dataSet!=null){
                        dataSet.clear();
                    }
                    if (productDetail.getCookBooks().size() > 0) {
                        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                    } else {
                        emptyLayout.setErrorType(EmptyLayout.NODATA);
                    }
                    dataSet.addAll(productDetail.getCookBooks());
                        adapter.setmData(dataSet);
                        adapter.notifyDataSetChanged();
                        name.setText("有关"+tit+"的美食");
                    } else {
                        emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                    }

            }

            @Override
            public void onFailure(Call<ResultDO<ProductDetail>> call, Throwable t) {
                dissMissDialog();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}

